package com.bskj.bms.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.utils.EncryptUtil;

public class BaseController {
	public BmsUser getSessionUser() throws Exception{
		Object sessionUser = getSession("user");
		if (sessionUser != null) {
			return JSONObject.parseObject(sessionUser.toString(), BmsUser.class);
		}
		return null;
	}

	public void setCookieUser(Map<String, Object> userInfo) {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();
		try {
			Cookie userCookie = new Cookie("user", EncryptUtil.enBase64(JSONObject.toJSONString(userInfo.get("user"))));
			userCookie.setMaxAge(30 * 24 * 3600);
			userCookie.setPath("/");
			Cookie roleNameCookie = new Cookie("roleName",
					EncryptUtil.enBase64(JSONObject.toJSONString(userInfo.get("roleName"))));
			roleNameCookie.setMaxAge(30 * 24 * 3600);
			roleNameCookie.setPath("/");
			Cookie menuTreeCookie = new Cookie("menuTree",
					EncryptUtil.enBase64(JSONObject.toJSONString(userInfo.get("menuTree"))));
			menuTreeCookie.setMaxAge(30 * 24 * 3600);
			menuTreeCookie.setPath("/");
			Cookie resourceListCookie = new Cookie("resourceList",
					EncryptUtil.enBase64(JSONObject.toJSONString(userInfo.get("resourceList"))));
			resourceListCookie.setMaxAge(30 * 24 * 3600);
			resourceListCookie.setPath("/");
			
			Cookie parkNameCookie=new Cookie("parkName", EncryptUtil.enBase64(JSONObject.toJSONString(userInfo.get("parkName"))));
			parkNameCookie.setMaxAge(30 * 24 * 3600);
			parkNameCookie.setPath("/");
			
			response.addCookie(roleNameCookie);
			response.addCookie(userCookie);
			response.addCookie(menuTreeCookie);
			response.addCookie(resourceListCookie);
			response.addCookie(parkNameCookie);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object getSession(String key) throws Exception{
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		Cookie[] cookies = request.getCookies();
		if(cookies != null) {
			for(Cookie cookie : cookies) {
				if(key.equals(cookie.getName())) {
					return JSONObject.parse(EncryptUtil.deBase64(cookie.getValue()));
				}
			}
		}
		return null;
	}

	public void removeAllSession() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();
		//移除cookie
		Cookie userCookie = new Cookie("user", null);
		Cookie roleNameCookie = new Cookie("roleName",null);
		Cookie menuTreeCookie = new Cookie("menuTree",null);
		Cookie resourceListCookie = new Cookie("resourceList",null);
		userCookie.setMaxAge(0);
		userCookie.setPath("/");
		roleNameCookie.setMaxAge(0);
		roleNameCookie.setPath("/");
		menuTreeCookie.setMaxAge(0);
		menuTreeCookie.setPath("/");
		resourceListCookie.setMaxAge(0);
		resourceListCookie.setPath("/");
		response.addCookie(roleNameCookie);
		response.addCookie(userCookie);
		response.addCookie(menuTreeCookie);
		response.addCookie(resourceListCookie);
	}
}
